import { createSlice } from "@reduxjs/toolkit";

const initialState = {
  value: [
    { 'id': 1, 'name': '小王', 'age': 18 },
    { 'id': 2, 'name': '小曹', 'age': 19 },
    { 'id': 3, 'name': '小邵', 'age': 20 },
  ]
}

export const listSlice = createSlice({
  name: 'list',
  initialState,
  reducers: {
    addRow: (state) => {
      state.value = [...state.value, {
        'id': 4, 'name': '小李', 'age': 21
      }]
    },
    deleteRow: (state, actions) => {
      const { id } = actions.payload
      state.value = state.value.filter((item) => item.id !== id)
    }
  }
})

export const { addRow, deleteRow } = listSlice.actions

export default listSlice.reducer